﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Reflection;

namespace BoAddonBiz_FreightForwarder
{
    public class B1Menus : SAPBoAddon.B1Menu
    {
        public B1Menus()
        {
            this.myMenuEvent += new myMenuEventEventHandler(B1Menu_MenuEvent);
        }

        void B1Menu_MenuEvent(SAPBoAddon.B1AddonBase.B1MenuEvent pVal, ref bool BubbleEvent)
        {
            if (!pVal.BeforeAction)
            {
                FieldInfo[] fields = typeof(B1FormTypes).GetFields();
                foreach (FieldInfo field in fields)
                {
                    string field_value = field.GetValue(new B1FormTypes()).ToString();

                    if (pVal.MenuUID.Length > 4)
                    {
                        if (pVal.MenuUID.Substring(pVal.MenuUID.Length - 4, 4).ToLower() == "find")
                        {
                            SAPBoAddon.B1FormInitializePar init = BoAddonBiz_FreightForwarder.ObjectFind.B1Form_Find.ShowForm(SBOApp.Forms.ActiveForm.UniqueID, pVal.MenuUID);
                            this.ShowForm(B1FormTypes.ado_AVA_ObjectFind, init);
                            break;
                        }
                        //先验证pVal.MenuUID.Length的长度 by Webb.Wen at 2011-06-01 14:21:22
                        if (pVal.MenuUID.Length >= 13 && pVal.MenuUID.Substring(0, 13).ToLower() == "AVA_BillDraft".ToLower())
                        {
                            SAPBoAddon.B1FormInitializePar init = new SAPBoAddon.B1FormInitializePar();
                            init.KeyValue = pVal.MenuUID;
                            this.ShowForm(B1FormTypes.ado_AVA_BillDraft, init);
                            break;
                        }
                    }
                    if (pVal.MenuUID == field_value)
                    {
                        this.ShowForm(field_value, SBOApp.Forms.ActiveForm.UniqueID);
                    }
                }
            }
        }

        protected override void AddMenuItems()
        {
            #region 其他
            ModifyMenu("2563", "客户跟踪");
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_Contract, "合同管理", SAPbouiCOM.BoMenuType.mt_STRING, "43535", 4);//业务合作伙伴

            ModifyMenu("3072", "费用项");
            ModifyMenu("3073", "费用项主数据");
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_ConditionTemp, "费用条件模板", SAPbouiCOM.BoMenuType.mt_STRING, "3072", 1);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_FEEScenario, "费用方案", SAPbouiCOM.BoMenuType.mt_STRING, "3072", 2);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_CargoManager, "货物管理", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.sys_Main, 9, SAPBoAddon.B1Addon.B1Addon.FolderIcon + "\\Cargo.bmp");//png works，ico doesn't
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_Port, "港口管理", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_CargoManager, 2);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_Dangerous, "危险品类别管理", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_CargoManager, 0);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_AirOrder_Main, "主单管理", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.sys_Main, 10, SAPBoAddon.B1Addon.B1Addon.FolderIcon + "\\AirOrder.bmp");// 
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_AirOrder, "主单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_AirOrder_Main, 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_AirOrderUsed, "主单领用", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_AirOrder_Main, 1);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_OrderNumber, "主单编码管理", SAPbouiCOM.BoMenuType.mt_STRING, "8192", 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_ApprovalStages, "审批级别", SAPbouiCOM.BoMenuType.mt_STRING, "14848", 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_ApprovalTemplates, "审批模板", SAPbouiCOM.BoMenuType.mt_STRING, "14848", 1);

            #endregion
            #region 空运业务菜单项
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_OrderMain, "空运操作", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.sys_Main, 11, SAPBoAddon.B1Addon.B1Addon.FolderIcon + "\\AirOperater.bmp");// 

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_Quote, "报价单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_QuoteFind, "报价查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 1);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_SalesOrder, "订单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 2);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_SalesOrderFind, "订单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 3);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_Delegate, "委托单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 4);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_DelegateFind, "委托单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 5);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_Prealert, "预告单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 6);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_PrealertFind, "预告单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 7);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_BusinessOperation, "业务单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 8);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_BusinessOperationFind, "业务单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_OrderMain, 9);

            #region 北京机场操作
            this.Menus.AddMenu("BJS", "北京机场", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.ado_AVA_FF_OrderMain, 10);
            this.Menus.AddMenu("BJS-Customs", "报关报检", SAPbouiCOM.BoMenuType.mt_POPUP, "BJS", 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_BJSClearCustoms, "进口报关报检", SAPbouiCOM.BoMenuType.mt_STRING, "BJS-Customs", 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_BJSClearCustomsFind, "进口报关报检查询", SAPbouiCOM.BoMenuType.mt_STRING, "BJS-Customs", 1);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_BJSApplyCustoms, "出口报关报检", SAPbouiCOM.BoMenuType.mt_STRING, "BJS-Customs", 2);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_BJSApplyCustomsFind, "出口报关报检查询", SAPbouiCOM.BoMenuType.mt_STRING, "BJS-Customs", 3);

            this.Menus.AddMenu("BJS-Warehouse", "监管库", SAPbouiCOM.BoMenuType.mt_POPUP, "BJS", 1);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_BJSWarehouse_IN, "监管库", SAPbouiCOM.BoMenuType.mt_STRING, "BJS-Warehouse", 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_BJSWarehouse_INFind, "监管库查询", SAPbouiCOM.BoMenuType.mt_STRING, "BJS-Warehouse", 1);
            //this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_BJSWarehouse_OUT, "出库", SAPbouiCOM.BoMenuType.mt_STRING, "BJS-Warehouse", 2);      
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_StorageImport, "仓储时间导入", SAPbouiCOM.BoMenuType.mt_STRING, "BJS-Warehouse", 2);      
            
            #endregion
            #region SDEL
            this.Menus.AddMenu("SEDL", "SEDL", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.ado_AVA_FF_OrderMain, 11);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SEDL, "SEDL数据项", SAPbouiCOM.BoMenuType.mt_STRING, "SEDL", 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SEDLFind, "SEDL数据项查询", SAPbouiCOM.BoMenuType.mt_STRING, "SEDL", 1);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SEDLExportForm, "SEDL数据导出", SAPbouiCOM.BoMenuType.mt_STRING, "SEDL", 2);
            #endregion
            #endregion
            #region 海运业务菜单项
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_OrderMain, "海运操作", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.sys_Main, 12, SAPBoAddon.B1Addon.B1Addon.FolderIcon + "\\SeaOperater.bmp");// 

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_Quote, "报价单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_QuoteFind, "报价查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 1);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_SalesOrder, "订单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 2);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_SalesOrderFind, "订单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 3);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_Delegate, "委托", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 4);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_DelegateFind, "委托查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 5);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_Prealert, "预告单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 6);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_PrealertFind, "预告单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 7);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_BusinessOpera, "业务单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 8);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_BusinessOperationFind, "业务单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_SE_OrderMain, 9);

            #region SDEL
            this.Menus.AddMenu("SE_SEDL", "海运SEDL", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.ado_AVA_SE_OrderMain, 10);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_SEDL, "SEDL数据项", SAPbouiCOM.BoMenuType.mt_STRING, "SE_SEDL", 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_SEDLFind, "SEDL数据项查询", SAPbouiCOM.BoMenuType.mt_STRING, "SE_SEDL", 1);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_SE_SEDLExportForm, "SEDL数据导出", SAPbouiCOM.BoMenuType.mt_STRING, "SE_SEDL", 2);
            #endregion

            #endregion
            #region 陆运业务菜单项
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_LN_OrderMain, "陆运操作", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.sys_Main, 13, SAPBoAddon.B1Addon.B1Addon.FolderIcon + "\\LandOperater.bmp");// 

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_LN_Quote, "报价单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_LN_OrderMain, 0);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_LN_QuoteFind, "报价查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_LN_OrderMain, 1);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_LN_SalesOrder, "订单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_LN_OrderMain, 2);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_LN_SalesOrderFind, "订单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_LN_OrderMain, 3);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_LN_Delegate, "委托单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_LN_OrderMain, 4);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_LN_DelegateFind, "委托单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_LN_OrderMain, 5);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_LN_BusinessOperation, "业务单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_LN_OrderMain, 6);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_LN_BusinessOperationFind, "业务单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_LN_OrderMain, 7);
            #endregion
            #region 其他业务菜单项
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_OR_OrderMain, "其他操作", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.sys_Main, 14, SAPBoAddon.B1Addon.B1Addon.FolderIcon + "\\OtherOperater.bmp");// 
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_OR_BusinessOperation, "业务单", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_OR_OrderMain, 1);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_OR_BusinessOperationFind, "业务单查询", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_OR_OrderMain, 2);

            #endregion
            #region 费用结算
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_BillDraft_AR, "对账单", SAPbouiCOM.BoMenuType.mt_STRING, "2048", 6);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_BillDraft_AP, "对账单", SAPbouiCOM.BoMenuType.mt_STRING, "2304", 6);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_Invoice, "电子发票", SAPbouiCOM.BoMenuType.mt_STRING, "2048", 7);

            this.Menus.AddMenu(B1MenuTypes.ado_AVA_OpenInvoiceARFind, "开票", SAPbouiCOM.BoMenuType.mt_STRING, "2048", 8);
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_OpenInvoiceAPFind, "开票", SAPbouiCOM.BoMenuType.mt_STRING, "2304", 7);
            #endregion

            #region 其他业务菜单项
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_DataExport, "数据导出", SAPbouiCOM.BoMenuType.mt_POPUP, B1MenuTypes.sys_Main, 15, "");
            this.Menus.AddMenu(B1MenuTypes.ado_AVA_FF_MPMonthForm, "MP业务月统计", SAPbouiCOM.BoMenuType.mt_STRING, B1MenuTypes.ado_AVA_FF_DataExport, 1);
            #endregion
        }

        private Dictionary<string, string> LoadDictionary()
        {
            XmlDocument xml = new XmlDocument();
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            //Menu标题键值对和语言包放在同一个路径下
            string xmlfile = String.Format(@"{0}{1}\{2}.xml", SAPBoAddon.B1Addon.B1Addon.LanguagePackageForm, SBOApp.Language, "Menus");
            if (File.Exists(xmlfile))//如果文件不存在则保持字典为空，对下面的操作没有影响
            {
                xml.Load(xmlfile);
                /* 
                 * 根据XPath遍历所有符合条件的节点
                 * 将MenuUID属性值读出来作为键，将MenuTitle属性读出来作为值
                 */
                XmlNodeList nodes = xml.SelectNodes("//MenuKeyValue");
                foreach (XmlNode node in nodes)
                {
                    string key = node.Attributes["MenuUID"] == null ? null : node.Attributes["MenuUID"].Value;
                    string value = node.Attributes["MenuTitle"] == null ? null : node.Attributes["MenuTitle"].Value;
                    if (!String.IsNullOrEmpty(key) && !String.IsNullOrEmpty(value) && !dictionary.ContainsKey(key))
                    {
                        dictionary.Add(key, value);
                    }
                }
            }
            return dictionary;
        }
        /// <summary>
        /// 修改现有菜单项的名称
        /// </summary>
        /// <param name="MenuID"></param>
        /// <param name="MenuName"></param>
        private void ModifyMenu(string MenuID, string MenuName)
        {
            SBOApp.Menus.Item(MenuID).String = MenuName;
        }
    }
}
